﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UT3Bots.UTItems
{

	public class UTVector
	{
		//Private Members
		private float _x;
		private float _y;
		private float _z;


		#region Properties
		public float X
		{
			get
			{
				return this._x;
			}
			set
			{
				this._x = value;
			}
		}

		public float Y
		{
			get
			{
				return this._y;
			}
			set
			{
				this._y = value;
			}
		}

		public float Z
		{
			get
			{
				return this._z;
			}
			set
			{
				this._z = value;
			}
		}
		#endregion


		//Constructor
		public UTVector(float x, float y, float z)
		{
			this._x = x;
			this._y = y;
			this._z = z;
		}


		/// <summary>
		/// Works out the distance between this UTVector and another. 
		/// (You should allow for a distance of about 300 if you are checking to see if you are at a specific location)
		/// </summary>
		/// <param name="a">The other UTVector to get the distance between</param>
		/// <returns></returns>
		public float DistanceFrom(UTVector a)
		{
			double distance, xx, yy, zz;

			xx = Math.Pow((_x - a.X), 2);
			yy = Math.Pow((_y - a.Y), 2);
			zz = Math.Pow((_z - a.Z), 2);
			distance = (float)Math.Sqrt(xx + yy + zz);

			if (double.IsNaN(distance))
			{
				distance = 0;
			}
			return (float)distance;
		}

        public double Magnitude
        {
            get
            {
                return Math.Sqrt((_x * _x) + (_y * _y) + (_z * _z));
            }
        }

		#region Public Overrides

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override string ToString()
		{
			return this._x + ", " + this._y + ", " + this._z;
		}


		public override bool Equals(object obj)
		{
			if (obj is UTVector)
			{
				if (this._x == ((UTVector)obj)._x &&
					this._y == ((UTVector)obj)._y &&
					this._z == ((UTVector)obj)._z)
				{
					return true;
				}
			}

			return false;
		}

		public static bool operator ==(UTVector a, UTVector b)
		{
			if (System.Object.ReferenceEquals(a, b))
			{
				return true;
			}

			// If one is null, but not both, return false.
			if (((object)a == null) || ((object)b == null))
			{
				return false;
			}

			// Return true if the fields match:
			if (a._x == b._x &&
				a._y == b._y &&
				a._z == b._z)
			{
				return true;
			}
			return false;
		}

		public static bool operator !=(UTVector a, UTVector b)
		{
			return !(a == b);
		}
		
		public static UTVector Parse(string toParse)
		{
			try
			{
				string[] parts = toParse.Split(UT3Bots.Communications.Message.MESSAGE_SUBSEPARATOR);
				for (int i = 0; i < parts.Length; i++)
				{
					if (parts[i] == "") parts[i] = "0.0";
				}
				float x = float.Parse(parts[0]);
				float y = float.Parse(parts[1]);
				float z = float.Parse(parts[2]);

				return new UTVector(x, y, z);
			}
			catch
			{
				throw new ArgumentException(string.Format("Unable to parse {0} to UTVector", toParse));
			}
		}
		#endregion

		
		
	}

}
